/**
 * @author Evgeny Sorokin
 */
Ext.define('USocial.util.Geolocation',{
	singleton : true,
	
	computeDistance : function(user1, user2){
		var lat1 = user1.get('lat'),
			lng1 = user1.get('lng'),
			lat2 = user2.get('lat'),
			lng2 = user2.get('lng');
		
		if(!Number.prototype.toRad){
			Number.prototype.toRad = function () { 
				return this * Math.PI / 180;
			}
		}
		
		var R = 6371; // km
		var dLat = (lat2-lat1).toRad();
		var dLon = (lng2-lng1).toRad();
		
		var lat1 = lat1.toRad();
		var lat2 = lat2.toRad();
		
		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		return R * c;
	},
	
	userLocationUpdated : function(user){
		var changes = user.getChanges();
		if(changes.lat || changes.lng){
			return true;
		}
		return false;
	}
	
});